package com.glodon.paas.account.dao.domain;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.glodon.paas.account.api.bean.AccountConstant;
import com.glodon.paas.account.api.bean.Project;
import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.account.service.util.AccountUtil;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Indexed;

@Entity(value = "user", noClassnameStored = true)
public class UserEntity {
	
	public static User getDto(UserEntity e, boolean cascade){
		if(e == null){
			return null;
		}
		else{
			User d = new User();
			d.setCreatedTime(e.getCreatedTime());
			d.setBirthday(e.getBirthday());
			d.setBirthdayType(e.getBirthdayType());
			d.setEmail(e.getEmail());
			d.setGender(e.getGender());
			d.setId(e.getId());
			d.setInActive(e.isInActive());
			d.setMobile(e.getMobile());
			d.setNickname(e.getNickname());
			d.setLoginname(e.getLoginname());
			d.setRealname(e.getRealname());			
			d.setUpdatedTime(e.getUpdatedTime());
			
			d.setDisplayName(e.displayName());
			d.setAccountName(e.accountName());
			d.setProjects(e.getProjects());
			if(cascade){
				AccountUtil.cascadeUser(e, d);
			}
			return d;
		}
	}

	@Id
	private String id;
		
	private String loginname;		//注册名，同loginName

	private String passwordSalt;	
	
	private String password;

	@Indexed
	private String email;
	
	private String mobile;
	
	
	//主账号类型，参见AccountConstant
	private Integer mainAccountType;

	private String nickname;
	
	private String realname;	

	private String gender;
	
	private Date birthday;
	
	private Integer birthdayType;	//1公历,2农历
	
	private String addressId;

	private String contactId;

	private String companyId;
	
	private Date createdTime;
	private Date updatedTime;
	
	private List<Project> projects;
	
	// 1: active, 0: inactive
	private Integer status = AccountConstant.USER_STATUS_ACTIVE;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	} 

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAddressId() {
		return addressId;
	}

	public void setAddressId(String addressId) {
		this.addressId = addressId;
	}

	public String getContactId() {
		return contactId;
	}

	public void setContactId(String contactId) {
		this.contactId = contactId;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
	
	public boolean isInActive(){
		return status.intValue() == 0;
	}

	public Date getUpdatedTime() {
		return updatedTime;
	}

	public void setUpdatedTime(Date updatedTime) {
		this.updatedTime = updatedTime;
	}

	public String getPasswordSalt() {
		return passwordSalt;
	}

	public void setPasswordSalt(String passwordSalt) {
		this.passwordSalt = passwordSalt;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Integer getBirthdayType() {
		return birthdayType;
	}

	public void setBirthdayType(Integer birthdayType) {
		this.birthdayType = birthdayType;
	}

	/**
	 * @return
	 */
	public String accountName() {
		String accountName = null;
		
		if(AccountConstant.ACCOUNT_NAME_EMAIL.equals(mainAccountType)){
			accountName = email;
		}else if(AccountConstant.ACCOUNT_NAME_MOBILE.equals(mainAccountType)){
			accountName = mobile;
		}else if(AccountConstant.ACCOUNT_NAME_NOMAL.equals(mainAccountType)){
			accountName = loginname;
		}
		
		if(accountName == null){
			accountName = email;
		}
		if(accountName == null){
			accountName = mobile;
		}
		if(accountName == null){
			accountName = loginname;
		}
		if(accountName == null){
			accountName = "null";
		}
		
		return accountName;
	}
 

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public Integer getMainAccountType() {
		return mainAccountType;
	}

	public void setMainAccountType(Integer mainAccountType) {
		this.mainAccountType = mainAccountType;
	}
	
	public void setProjects(List<Project> projects) {
        this.projects = projects;
    }

	public List<Project> getProjects() {
        return projects;
    }

	/**
	 * @return
	 */
	public String displayName() {
		if(StringUtils.isNotBlank(nickname)){
			return nickname;
		}
		if(StringUtils.isNotBlank(realname)){
			return realname;
		}
		return accountName();
	}
	@Override
	public String toString() {
	    return ToStringBuilder.reflectionToString(this);
	}
}
